/** Mode handler for a sector map.
@param map The sector map to use.
*/
function SectorMapMode(map)
{
	this._map = map;
	this._map.generate();
	this.render();
	logWindow.pushMessage("%f0Entering " + map.name);
}
SectorMapMode.prototype =
{
	/** Handle an input action..
	 *@param action The action string to handle.
	 */
	handleAction: function(action)
	{
		switch(action)
		{
			case "up": this._doMove(0, -1); break;
			case "down": this._doMove(0, 1); break;
			case "left": this._doMove(-1, 0); break;
			case "right": this._doMove(1, 0); break;
			case "look":
				modeStack.push(
					new SelectPositionMode(
						this._map,
						player.x,
						player.y,
						0,
						this._lookCallback,
						this
					)
				);
				return true;
				break;
			case "use":
				// Try to pick up an item
				var ents = this._map.getEntitiesAtPosition(player.x, player.y);
				for(var i = ents.length - 1; i >= 0; --i)
				{
					var ent = ents[i];
					if(ent.isItem)
					{
						player.addEntity(ent);
						logWindow.pushMessage("%f0You picked up " + ent.article + " " + ent.name + ".");
					}
				}
				break;
			case "inventory":
				modeStack.push(new InventoryMode());
				return true;
				break;
			default: return true; break;
		}
		this._doStatusLine();
		return true;
	},
	/** Look command callback
	*/
	_lookCallback: function(x, y, canceled)
	{
		if(canceled)
			return;
		logWindow.pushMessage("You see " + this._map.getPositionDescription(x, y));
	},
	/** Move the player.
	@param xOfs X offset.
	@param yOfs Y offset.
	*/
	_doMove: function(xOfs, yOfs)
	{
		var nx = player.x + xOfs;
		var ny = player.y + yOfs;

		// Handle the player leaving the map
		if(nx < 0 ||
			ny < 0 ||
			nx >= this._map.width ||
			ny >= this._map.height)
		{
			this._map.removeEntity(player);
			worldMap.onPlayerReenter(player);
			removeMode(this);
			modeStack.push(worldMapMode);
			worldMapMode.render();
			return;
		}
		// Normal move
		if(this._map.tryMoveEntity(player, nx, ny))
		{
			logWindow.clearMessage();
			this._map.doTurn();
		}
		// Bumped into something
		else
		{
			var ents = this._map.getEntitiesAtPosition(nx, ny);
			for(var i = ents.length - 1; i >= 0; --i)
			{
				if(ents[i].blockWalk)
				{
					logWindow.clearMessage();
					var ent = ents[i];
					// If it's a mobile attack it
					if(ent.isMobile)
						player.attack(ent);
					// Otherwise try to use it
					else
						ents[i].use(player);
					this._map.doTurn();
					break;
				}
			}
		}
	},
	/** Create the "stuff under me" message if appropriote.
	 */
	_doStatusLine: function()
	{
		if(!logWindow.isEmpty())
			return;
		var ents = this._map.getEntitiesAtPosition(player.x, player.y);
		var desc = "";
		for(var i = 0; i < ents.length; ++i)
		{
			var ent = ents[i];
			if(ent == player)
				continue;
			if(desc.length > 0)
				desc += ", ";
			desc += ent.article + " " + ent.name;
		}
		if(desc.length > 0)
			logWindow.tempMessage("%f0" + desc);
	},
	/** Rerender everything.
	*/
	render: function()
	{
		this._map.render();
	}
};
